package demo7;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86185
 * Date: 2023-11-24
 * Time: 16:01
 */
public class Main {
    private static int getSum(String string) {
        int sum = 0;
        int n = Math.min(string.length(), 3);
        for(int i = 0; i < n; i++) {
            char c = string.charAt(string.length() - 1 - i);
            sum += Math.pow(32, i) * (c - 'A');
        }
        return sum;
    }
    static int flag;
    static int p;
    static HashMap<String, Integer> map = new HashMap<>();

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        p = scanner.nextInt();
        int[] num = new int[p];
        for(int i = 0; i < n; i++) {
            String string = scanner.next();
            if(map.containsKey(string)) {
                print(map.get(string));
                continue;
            }
            int sum = getSum(string);
            int index = searchIndex(sum, num);
            map.put(string, index);
            insert(index, num);
        }
    }

    private static int searchIndex(int sum, int[] num) {
        int index = sum % p;
        if(num[index] == 0) {
            return index;
        } else {
            return squaringMethod(index, num);
        }
    }

    private static void print(int index) {
        if(flag == 0) {
            flag = 1;
            System.out.print(index);
        } else {
            System.out.print(" " + index);
        }
    }
    private static void insert(int index, int[] num) {
            num[index] = 1;
            print(index);
    }

    private static int squaringMethod(int index, int[] num) {
        int t = 1;
        for (; ; t++) {
            int in = (int) Math.pow(t, 2);
            int zin = (in + index) % p;
            int fin = (index - in + p) % p;
            if(num[zin] == 0) {
                return zin;
            } else if (num[fin] == 0){
                return fin;
            }
        }
    }
}
